package com.hzyl.famousreader.view.fragment;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzyl.famousreader.Constant;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.ThemeColor;
import com.hzyl.famousreader.repository.persistence.entity.Book;
import com.hzyl.famousreader.router.Router;
import com.hzyl.famousreader.view.adapter.BookshelfAdapter;
import com.hzyl.famousreader.viewmodel.BookViewModel;
import com.hzyl.famousreader.viewmodel.MainViewModel;
import com.hzyl.famousreader.databinding.*;
import java.util.List;

/**
 * 书架
 */
@Route(path = "/main/bookshelf")
public class BookshelfFragment extends BaseFragment {

  private FragmentBookshelfBinding binding;

  private BookshelfAdapter bookshelfAdapter;

  private MainViewModel viewModel;

  private BookViewModel bookViewModel;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bookshelf, container, false);

    int colorId = SPUtils.getInstance().getInt(Constant.SP_THEME_COLOR,  ThemeColor.DEFAULT.getColor());
    BarUtils.setStatusBarColor(getActivity(), colorId, 0);
    binding.toolbar.setBackgroundColor(colorId);

    viewModel = ViewModelProviders.of(getActivity()).get(MainViewModel.class);
    bookViewModel = ViewModelProviders.of(getActivity()).get(BookViewModel.class);

    binding.tipWrapper.setOnClickListener(v -> viewModel.fragmentIndicator.setValue(R.id.navigation_shop));

    bookshelfAdapter = new BookshelfAdapter(null);
    binding.bookshelfRvBook.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    binding.bookshelfRvBook.setAdapter(bookshelfAdapter);

    //删除书架书籍
    bookshelfAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        new SweetAlertDialog(getContext(), SweetAlertDialog.WARNING_TYPE)
          .setTitleText("确认删除书籍?")
          //.setContentText("Won't be able to recover this file!")
          .setConfirmText("删除")
          .setCancelText("取消")
          .showCancelButton(true)
          .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sDialog) {
              List<Book> books = adapter.getData();
              bookViewModel.deleteBook(books.get(position).getId());
              sDialog.dismissWithAnimation();
            }
          })
          .show();
        return true;
      }
    });

    //阅读书籍
    bookshelfAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<Book> books = adapter.getData();
        Book book = books.get(position);
        ARouter.getInstance()
          .build(Router.ACTIVITY_READ)
          .withObject("book", book)
          .withString("bookName", book.getName())
          .navigation();
        bookViewModel.updateLastReadDate(book.getId());
      }
    });

    //获取书架书籍
    bookViewModel.bookshelves().observe(this, new Observer<List<Book>>() {
      @Override
      public void onChanged(@Nullable List<Book> books) {
        if (books != null && books.size() > 0) {
          binding.tipWrapper.setVisibility(View.GONE);
          bookshelfAdapter.setNewData(books);
        } else {
          binding.tipWrapper.setVisibility(View.VISIBLE);
          bookshelfAdapter.setNewData(null);
        }
      }
    });

    return binding.getRoot();
  }
}
